package com.ehcache.test;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;
import org.youyun56.gpsserver.common.PathConfig;
import org.youyun56.gpsserver.common.RSAManager;

import com.j3.db.ConnectionManager;
import com.j3.sql.GridUtils;
import com.j3.sql.SqlFunc;

public class ReplaceCarLength {
    @Test
    public void replaceCarLength() throws Exception {
        Properties props = new Properties();
        props.load(new FileInputStream(PathConfig.ROOT + File.separator + "config.properties"));
        RSAManager manager = new RSAManager(PathConfig.ROOT + File.separator + "oid");
        props.put("password", manager.decrypt(props.getProperty("password")));
        DataSource bds = BasicDataSourceFactory.createDataSource(props);
        ConnectionManager.registerDataSource("gps", bds);
        ConnectionManager.setDefaultDataSourceName("gps");
        GridUtils grid = new GridUtils();
        grid.init("select * from car");
        Pattern pattern = Pattern.compile("[0-9.]+");

        while (grid.next()) {
            String carLength = grid.get("carLength");
            System.out.print(carLength);
            Matcher matcher = pattern.matcher(carLength);
            if (matcher.find()) {
                String carLen = matcher.group();
                System.out.println(carLen);
                SqlFunc fun = new SqlFunc();
                fun.executeUpdate("update car set carLength = " + carLen + " where id = " + grid.get("id"));
            }

        }
    }

    @Test
    public void getA() {

    }
}
